package miiteen.customsportal.login.service;

import cn.gov.customs.casp.config.ConfigReader;
import cn.gov.customs.casp.sdk.h4a.BeanReaderHelper;
import cn.gov.customs.casp.sdk.h4a.entity.ObjectParentOrganizations;
import cn.gov.customs.casp.sdk.h4a.entity.ObjectsDetail;
import cn.gov.customs.casp.sdk.h4a.enumdefines.ViewCategory;
import cn.gov.customs.casp.sdk.h4a.ogu.ws.IOguReaderGetObjectParentOrganizationsCupaaFaultArgsFaultFaultMessage;
import cn.gov.customs.casp.sdk.h4a.ogu.ws.IOguReaderGetObjectsDetailCupaaFaultArgsFaultFaultMessage;
import cn.gov.customs.casp.sdk.h4a.ogu.ws.ObjectCategory;
import cn.gov.customs.casp.sdk.h4a.ogu.ws.OrganizationCategory;
import cn.gov.customs.casp.sdk.h4a.passport.IOguBeanReader;
import cn.gov.customs.casp.sdk.h4a.sso.IPassportSSO;
import cn.gov.customs.casp.sdk.h4a.sso.passport.Ticket;
import cn.gov.customs.casp.sdk.h4a.sso.passport.util.CookieCenter;
import cn.gov.customs.casp.sdk.h4a.util.H4aDefaultConstants;
import miiteen.customsportal.basic.beans.BasicPageBrowsing;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class SamplePassportSSO implements IPassportSSO {

    @Override
    public void LogOut(HttpServletRequest request, HttpServletResponse response) {
        CookieCenter cc = new CookieCenter(request, response);
        HttpSession session = request.getSession(true);
        session.setAttribute("TICKET", null);
        cc.clearCookie();
    }

    @Override
    public void Login(Ticket ticket, HttpServletRequest request, HttpServletResponse response, String time) {
        HttpSession session = request.getSession(true);
        StringBuffer userId = new StringBuffer();
        userId.append(ticket.getLn());
        userId.append("&");
        userId.append(ticket.getAbm());
        //获取登录信息
        String objectDetailsLastParam = ConfigReader.getConfigValue("h4a-config.xml", "object_param", "parameter_obj", "getBeanObjectsDetail_lastParam");
        String organizationLastParam = ConfigReader.getConfigValue("h4a-config.xml", "object_param", "parameter_obj", "organizationCategory_lastParam");
        if (null == objectDetailsLastParam) {
            objectDetailsLastParam = "";
        }
        if (null == organizationLastParam) {
            organizationLastParam = "";
        }
        IOguBeanReader oguBeanReaderHelper = BeanReaderHelper.getIOguBeanReader();
        try {
            ObjectsDetail[] objectDetails = oguBeanReaderHelper.getBeanObjectsDetail(
                    H4aDefaultConstants.DEFAULT_BASE_VIEW,
                    ViewCategory.ViewCode, userId.toString(),
                    ObjectCategory.USER_IDENTITY, "",
                    OrganizationCategory.NONE, objectDetailsLastParam);
            if (null != objectDetails && objectDetails.length > 0) {
                ObjectsDetail objectDeatil = objectDetails[0];//这里为了编写示例默认取第一个人员，默认第一个是主职，其他都是兼职，
                //如果业务系统需要使用到兼职用户需要自行编写业务逻辑实现兼职用户的情况
                session.setAttribute("user", objectDeatil);
                session.setAttribute("userIP", request.getRemoteAddr());
                ObjectParentOrganizations[] objectOrganizations = oguBeanReaderHelper.getBeanObjectParentOrganizations(
                        H4aDefaultConstants.DEFAULT_BASE_VIEW,
                        ViewCategory.ViewCode, objectDeatil.getAll_path_name(),
                        ObjectCategory.USER_ALL_PATH_NAME, "",
                        OrganizationCategory.NONE, false, false,
                        "", organizationLastParam);
                if (null != objectOrganizations && objectOrganizations.length > 0) {
                    session.setAttribute("organization", objectOrganizations);
                }
            }


        } catch (IOguReaderGetObjectsDetailCupaaFaultArgsFaultFaultMessage | IOguReaderGetObjectParentOrganizationsCupaaFaultArgsFaultFaultMessage /*| IAccreditReaderGetFunctionsOfUserCupaaFaultArgsFaultFaultMessage*/ e) {
            e.printStackTrace();
        }
    }
}
